from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QVBoxLayout,QWidget

class SecondWindow(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUI()

    def initUI(self):
        self.setWindowTitle('第二个窗口')
        self.setGeometry(300, 300, 250, 150)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('主窗口')
        self.setGeometry(100, 100, 250, 150)

        # 创建一个按钮
        self.button = QPushButton('跳转到第二个窗口')
        self.button.clicked.connect(self.openSecondWindow)

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(self.button)

        # 设置中央部件的布局
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

    def openSecondWindow(self):
        # 实例化第二个窗口并显示
        # 注意：这里我们传递了 self（主窗口）作为父窗口
        # 但第二个窗口仍然会保持打开，即使主窗口关闭
        second_window = SecondWindow(self)
        second_window.show()
        self.close()

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())